/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import conf.HibernateUtil;
import java.util.List;
import models.Twitterer;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

/**
 *
 * @author Misja
 */
public class CreateUser {

    public CreateUser() {
    }

    @BeforeClass
    public static void setUpClass() {
    }

    @AfterClass
    public static void tearDownClass() {
    }

    @Before
    public void setUp() {
    }

    @After
    public void tearDown() {
    }
    // TODO add test methods here.
    // The methods must be annotated with annotation @Test. For example:
    //
    // @Test
    // public void hello() {}

    @Test
    public void createRetrieveTwitterer() {
        Twitterer tw1 = new Twitterer("misja", "pswd");
        Twitterer tw2 = new Twitterer("chris", "pswd");
        Twitterer tw3 = new Twitterer("dick", "pswd");
        Twitterer tw4 = new Twitterer("ingrid", "pswd");
        Twitterer tw5 = new Twitterer("raoul", "pswd");
           
        tw1.setFavorite(tw2);
        tw1.setFavorite(tw3);
        tw1.setFavorite(tw4);
        
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();

        try {
           session.save(tw1);
            session.save(tw2);
           session.save(tw3);
           session.save(tw4);
           session.save(tw5);
            
            List<Twitterer> result;
            
            result = session.createQuery("from Twitterer").list(); 
             Query query = session.createQuery("from Twitterer where userName = :userName and password =:password");
            query.setParameter("userName", "misja");
            query.setParameter("password", "pswd");
            Twitterer twitterer = (Twitterer)query.list().get(0); 
            
            session.getTransaction().commit();
        } catch (HibernateException e) {
            System.out.println("HibernateFout: " + e);
            session.close();
            assert false;
        }
        
        assert true;
    }
}